package com.feitubc.tools.fileimport.strategy;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.feitubc.tools.fileimport.IProcessor;
import com.feitubc.tools.fileimport.StrategyContext;
import com.feitubc.tools.fileimport.domain.ProcessorMethodEnum;
import com.google.common.collect.Lists;

/**
 * 说明：任务处理代理工厂类，是否多线程处理
 * @author zhc
 */
public class ProcessorProxyFactory {

	/**
	 * 代理任务处理器
	 */
	private IProcessor target;
	
	/**
	 * 上下文
	 */
	private StrategyContext strategyContext;
	
	public ProcessorProxyFactory(StrategyContext strategyContext, IProcessor targetProcessor) {
		this.strategyContext = strategyContext;
		if (strategyContext.getFileStrategySetting().isUseMultiThread()) {
			// 使用多线程处理
			this.target = new MultiThreadProcessor(targetProcessor);
		} else {
			this.target = targetProcessor;
		}
	}
	
	/**
	 * 根据不同方法枚举准备数据 并执行
	 * @param processorMethod
	 * @return
	 */
	public boolean doExecute(ProcessorMethodEnum processorMethod) {
		List<?> executeData = null;
		if (ProcessorMethodEnum.preData.equals(processorMethod)) {
			TreeMap<Integer, Map<Integer, Object>> dataMap = strategyContext.getFileDataMap();
			executeData = Lists.newArrayList(dataMap.entrySet());
		} else if (ProcessorMethodEnum.executeImport.equals(processorMethod)) {
			executeData = Lists.newArrayList(strategyContext.getExecuteDataList());
		} else if (ProcessorMethodEnum.saveOperateLogMulti.equals(processorMethod)) {
			executeData = Lists.newArrayList(strategyContext.getImportLogObjects());
		}
		return this.target.doExecute(strategyContext, executeData, processorMethod);
	}
	
	/**
	 * 工程方法处理
	 * @param strategyContext
	 * @param targetProcessor
	 * @param processorMethod
	 * @return
	 */
	public static boolean doExecute(StrategyContext strategyContext, IProcessor targetProcessor, ProcessorMethodEnum processorMethod) {
		ProcessorProxyFactory factory = new ProcessorProxyFactory(strategyContext, targetProcessor);
		return factory.doExecute(processorMethod);
	}
}
